﻿/*
 *  PirateShare - a tool for fair&legal content sharing among friends.  
 *  see <http://pirateshare.codeplex.com> for latest version.
 *
 *  Copyright (C) 2010 Ilya Elenskiy
 *
 *  This program is free software; you can redistribute it and/or modify 
 *  it under the terms of the GNU General Public License as published by 
 *  the Free Software Foundation; either version 2 of the License, or 
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful, but 
 *  WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License 
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Timers;
using System.Windows.Threading;
using System.Threading;

namespace PirateShare
{
    static class Globals
    {
        public const int FileSegmentSize = 128 * 1024;  //128 kb segments
        public const int IndexUpdateDelayMS = 15 * 60 * 1000; //every 15 minutes

        public static string workingDir;
        public static MainWindow mainWindow;


        public static volatile User ThisUser;


        public static DispatcherTimer IndexUpdateTimer;
        public static IndexWorker indexWorker;
        public static Thread indexWorkerThread;
        

        public static void SaveUser()
        {
            if (ThisUser == null) { throw new Exception("No user to serialize"); }

            string userFile = Globals.workingDir + Path.DirectorySeparatorChar + "user.bin";

            BinaryFormatter bf = new BinaryFormatter();
            FileStream stream = new FileStream(userFile, FileMode.Create);

            bf.Serialize(stream, ThisUser);

            stream.Close();
            stream.Dispose();
        }

        public static bool LoadUser()
        {
            string userFile = Globals.workingDir + Path.DirectorySeparatorChar + "user.bin";


            if (!File.Exists(userFile))
            {
                return false;
            }

            
            BinaryFormatter bf = new BinaryFormatter();
            FileStream stream = new FileStream(userFile, FileMode.Open);

            ThisUser = (User)bf.Deserialize(stream);

            stream.Close();
            stream.Dispose();

            return true;
        }


        public static void InitIndexWorker()
        {
            indexWorker = new IndexWorker();
            indexWorkerThread = new Thread(new ThreadStart(indexWorker.runIndex));
            indexWorkerThread.Priority = ThreadPriority.Lowest;

            IndexUpdateTimer = new DispatcherTimer(DispatcherPriority.Background);
            IndexUpdateTimer.Interval = TimeSpan.FromMilliseconds(Globals.IndexUpdateDelayMS);
            IndexUpdateTimer.Tick += new EventHandler(IndexWorkerTick);
            IndexUpdateTimer.Start();

            IndexWorkerTick(null, null);
        }

        public static void IndexWorkerTick(object sender, EventArgs e)
        {
            if (!indexWorkerThread.IsAlive)
            {
                SaveUser();

                indexWorkerThread = new Thread(new ThreadStart(indexWorker.runIndex));
                indexWorkerThread.Priority = ThreadPriority.Lowest;
                indexWorkerThread.Start();
            }
        }

        public static void InitNetworking()
        {

        }
    }
}
